<template>
  <div class="login-page">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="welcome-content">
          <h1>智护银龄</h1>
          <h2>社区智慧养老健康监护系统</h2>
          <p class="subtitle">为社区老年人提供全方位的健康监护服务</p>
          <div class="features">
            <div class="feature-item">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
                  <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
                </svg>
              </div>
              <div class="feature-text">
                <h3>实时健康监控</h3>
                <p>24小时监测老人健康数据</p>
              </div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
                  <path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z"/>
                </svg>
              </div>
              <div class="feature-text">
                <h3>智能预警系统</h3>
                <p>异常情况及时预警通知</p>
              </div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
                  <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                </svg>
              </div>
              <div class="feature-text">
                <h3>社区人员管理</h3>
                <p>统一管理社区老人信息</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-container">
          <div class="login-header">
            <h2>社区用户登录</h2>
            <p class="header-subtitle">请输入您的用户名和密码</p>
          </div>
          <form @submit.prevent="handleLogin">
            <div class="form-group">
              <label for="username">用户名</label>
              <div class="input-wrapper">
                <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                  <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                </svg>
                <input 
                  v-model="username" 
                  id="username" 
                  type="text" 
                  required 
                  placeholder="请输入用户名"
                />
              </div>
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <div class="input-wrapper">
                <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                  <path d="M12 1L3 5v6c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V5l-9-4zm0 10.99h7c-.53 4.12-3.28 7.79-7 8.94V12H5V6.3l7-3.11v8.8z"/>
                </svg>
                <input 
                  v-model="password" 
                  id="password" 
                  type="password" 
                  required 
                  placeholder="请输入密码"
                />
              </div>
            </div>
            <button type="submit" class="login-btn">
              <span>登 录</span>
            </button>
            <div class="switch-login">
              <a @click="switchToAdmin">切换到管理员登录</a>
            </div>
            <div class="switch-login">
              <a @click="goToForgotPassword">忘记密码</a>
            </div>
          </form>
          <p v-if="error" class="error">{{ error }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import request from '../utils/request'
import { setToken, setUserInfo } from '../utils/auth'

const username = ref('')
const password = ref('')
const error = ref('')
const router = useRouter()

const handleLogin = async () => {
  error.value = ''
  try {
    const res = await request.post('/user/login', {
      username: username.value,
      password: password.value
    })
    
    // 保存token和用户信息
    setToken(res.data.token)
    setUserInfo({
      name: res.data.name
    })
    
   
    
    router.push('/home')
  } catch (e) {
    error.value = e.message || '登录失败，请稍后重试'
    console.error('登录错误:', e)
  }
}

const switchToAdmin = () => {
  router.push('/adminlogin')
}
const goToForgotPassword = () => {
  router.push('/forgot-password')
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-wrapper {
  display: flex;
  width: 100%;
  max-width: 1200px;
  height: 700px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.2);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: "";
  position: absolute;
  top: -50px;
  left: -50px;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.login-left::after {
  content: "";
  position: absolute;
  bottom: -80px;
  right: -80px;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.welcome-content {
  color: white;
  text-align: center;
  position: relative;
  z-index: 1;
  max-width: 500px;
}

.welcome-content h1 {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 10px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.welcome-content h2 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 15px;
}

.subtitle {
  font-size: 18px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.features {
  text-align: left;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 30px;
}

.feature-icon {
  width: 50px;
  height: 50px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  flex-shrink: 0;
}

.feature-text h3 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 5px;
}

.feature-text p {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background: white;
}

.login-container {
  width: 100%;
  max-width: 450px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 32px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.header-subtitle {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.form-group {
  margin-bottom: 25px;
}

label {
  display: block;
  margin-bottom: 10px;
  color: #333;
  font-weight: 500;
  font-size: 16px;
}

.input-wrapper {
  position: relative;
}

.input-wrapper svg {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
}

.input-wrapper input {
  width: 100%;
  height: 55px;
  padding: 0 20px 0 50px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s;
  box-sizing: border-box;
}

.input-wrapper input:focus {
  border-color: #4facfe;
  outline: none;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.login-btn {
  width: 100%;
  height: 55px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(79, 172, 254, 0.3);
  margin-top: 10px;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(79, 172, 254, 0.4);
}

.login-btn:active {
  transform: translateY(0);
}

.switch-login {
  text-align: center;
  margin-top: 25px;
}

.switch-login a {
  color: #666;
  font-size: 16px;
  text-decoration: none;
  cursor: pointer;
  transition: color 0.3s;
}

.switch-login a:hover {
  color: #4facfe;
}

.error {
  color: #ff6b6b;
  text-align: center;
  margin-top: 20px;
  font-size: 16px;
  font-weight: 500;
}

@media (max-width: 992px) {
  .login-wrapper {
    flex-direction: column;
    height: auto;
  }
  
  .login-left {
    padding: 30px 20px;
  }
  
  .login-right {
    padding: 30px 20px;
  }
  
  .welcome-content h1 {
    font-size: 36px;
  }
  
  .welcome-content h2 {
    font-size: 24px;
  }
}

@media (max-width: 576px) {
  .login-page {
    padding: 10px;
  }
  
  .login-wrapper {
    border-radius: 15px;
  }
  
  .login-header h2 {
    font-size: 28px;
  }
  
  .input-wrapper input {
    height: 50px;
    padding: 0 15px 0 45px;
  }
}
</style>